/* tslint:disable:no-shadowed-variable */

export const TOAST_TYPES: any = {
  Success: "success",
  Warning: "warning",
  Error: "error"
};

export interface ToastConfig {
  title?: string;
  message?: string;
  type?: "success" | "warning" | "error" | "blank";
}

const state = () => ({
  show: false,
  title: "",
  message: "",
  type: "",
  duration: 3000,
});

const getters = {
  show: (state: any) => {
    return state.show;
  },
  title: (state: any) => {
    return state.title;
  },
  message: (state: any) => {
    return state.message;
  },
  type: (state: any) => {
    return state.type;
  },
  duration: (state: any) => {
    return state.duration;
  },
  isSuccess: (state: any) => {
    return state.type === TOAST_TYPES.Success;
  },
  isWarning: (state: any) => {
    return state.type === TOAST_TYPES.Warning;
  },
  isError: (state: any) => {
    return state.type === TOAST_TYPES.Error;
  }
};

const mutations = {
  setTitle(state: any, title: string): void {
    state.title = title;
  },
  setMessage(state: any, message: string): void {
    state.message = message;
  },
  setType(state: any, type: string): void {
    state.type = type;
  },
  setShow(state: any, show: boolean): void {
    state.show = show;
  }
};

const actions = {
  makeToast({ commit }: any, { title, message, type }: ToastConfig): void {
    commit("setTitle", title);
    commit("setMessage", message);
    commit("setType", type);
    commit("setShow", true);
  },
  dismiss({ commit }: any): void {
    commit("setShow", false);
  }
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
};
